package com.wzdigit.wms.raw.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.SearchInvInitTaskDataPdaRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitInvInitTaskDataPdaRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LocationApiResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskDataPdaResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskQtyResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskScanSnPdaResponse;
import com.wzdigit.wms.basic.domain.dtos.inventory.PrintSnDataDto;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.inv.InvInitTaskDataPdaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 库存初始化---PDA端
 * @author Administrator
 *
 */
@RestController
@Api(tags = "库存初始化---PDA端")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class InvInitTaskDataPdaController {
	
	public static final String REQUEST_MODEL = "/invinittaskdatapda";

    @Autowired
    private InvInitTaskDataPdaService invInitTaskDataPdaService;

    @ApiOperation(value = "查询", notes = "查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectData")
    public PagerInfo<List<InvInitTaskDataPdaResponse>> selectData(
    		SearchInvInitTaskDataPdaRequest dto, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    	try {
    		return invInitTaskDataPdaService.selectData(dto, pager, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "刷新数量（账目、实际）", notes = "刷新数量（账目、实际）")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/refreshQty")
    public InvInitTaskQtyResponse refreshQty(
    		@ApiParam(value = "主键ID", required = true) 
    		@RequestParam(required = true) Integer invInitId, 
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    	try {
    		return invInitTaskDataPdaService.refreshQty(invInitId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "扫描库位", notes = "扫描库位")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/scanLoc")
    public LocationApiResponse scanLoc(
    		@ApiParam(value = "仓库ID", required = true) 
    		@RequestParam(required = true) Integer whsId, 
    		@ApiParam(value = "仓库名称", required = true) 
    		@RequestParam(required = true) String whsName, 
    		@ApiParam(value = "库位编码", required = true) 
    		@RequestParam(required = true) String locCode, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		return invInitTaskDataPdaService.scanLoc(whsId, whsName, locCode);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "扫描条码", notes = "扫描条码")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/scan")
    public InvInitTaskScanSnPdaResponse scan(
    		@ApiParam(value = "主键ID", required = true) 
    		@RequestParam(required = true) Integer invInitId, 
    		@ApiParam(value = "二维码", required = true) 
    		@RequestParam(required = true) String qrRuleParam, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		return invInitTaskDataPdaService.scan(invInitId, qrRuleParam);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "拆分条码", notes = "拆分条码")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/split")
    public PrintSnDataDto split(
    		@ApiParam(value = "二维码", required = true) 
    		@RequestParam(required = true) String qrRuleParam, 
    		@ApiParam(value = "预留数（账面-实际-操作）", required = true) 
    		@RequestParam(required = true) BigDecimal removeQty, 
    		@ApiParam(value = "仓库名称", required = true) 
    		@RequestParam(required = true) String whsName, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		return invInitTaskDataPdaService.split(qrRuleParam, removeQty, whsName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "提交", notes = "提交")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/submit")
    public Result<String> submit(
    		@RequestBody @Valid SubmitInvInitTaskDataPdaRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		ValidatorUtil.validator(bindingResult);
    		return invInitTaskDataPdaService.submit(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}